var ns, toast;
var app = new Vue({
    el: '#app',
    data: {
        id: 0,
        orderlist: [],
        //状态：0-全部 1-待付款 5-待发货 2-待收货
        statusList: [
            {
                id: 0,
                text: '全部',
                count: 0,
            }, {
                id: 5,
                text: '待发货',
                count: 0,
            }, {
                id: 2,
                text: '待收货',
                count: 0,
            }, {
                id: 3,
                text: '已完成',
                count: 0,
            }, {
                id: 4,
                text: '已取消',
                count: 0,
            }
        ],
        statuscount: {},
        status: '0',
        keywords: '',
        form: {
            pageindex: 0,
            pagesize: 10,
        },
        can_scroll: false,
        current_item_length: 0,
        startTime: '',
        endTime: '',
        show: false,
    },
    methods: {
        get_data: function () {
            var that = this;
            that.get_List();
        },
        get_List: function () {
            var that = this;
            that.can_scroll = false;
            var opt = {
                action: 'getorderlist',
                status: that.status,
                keywords: that.keywords,
                pageSize: that.form.pagesize,
                startRowIndex: that.form.pageindex,
                startTime: that.startTime,
                endTime: that.endTime,
            };
            ns.post(opt, function (succeed, data, err) {
                // console.log(JSON.stringify(data));
                if (succeed) {
                    if (data.status) {
                        if (data.data.List.length == that.form.pagesize) {
                            that.can_scroll = true;
                        }
                        if (that.form.pageindex == 0) {
                            that.orderlist = data.data.List;
                        } else {
                            that.orderlist = that.orderlist.concat(data.data.List);
                        }
                        that.current_item_length = that.orderlist.length;

                        //顶部状态数量赋值
                        that.statuscount = data.data.Summaty;
                        that.statusList[1].count = that.statuscount.NoSendCount;
                        that.statusList[2].count = that.statuscount.SendCount;
                    }

                } else if (err) {
                    that.orderlist = [];
                    that.current_item_length = 0;
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }

            })
        },
        //导航栏切换
        randomSwitchBtn: function (id) {
            var that = this;
            //调用接口给数据赋值
            that.form.pageindex = 0;
            that.form.pagesize = 10;
            that.status = id;
            that.get_List();

        },
        tel_phone: function (telPhone) {
            if (!telPhone || telPhone == undefined) {
                api.toast({
                    msg: '电话号码为空',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            api.call({
                type: 'tel_prompt',
                number: telPhone
            });
        },
        //订单详情
        open_order_detail: function (id) {
            var that = this;
            ns.openWin('dl_order_detail', '订单详情', {
                id: id,
            });
        },
        //取消订单
        do_removeorder: function (id) {
            var that = this;
            that.id = id;
            that.show = true;
        },
        beforeClose: function (action, done) {
            var that = this;
            if (action === 'confirm') {
                var option = {};
                option.OrderID = that.id;
                option.action = 'cancelorder';
                ns.post(option, function (succeed, data, err) {
                    if (succeed) {
                        if (data.status) {
                            api.toast({
                                msg: '操作成功',
                                duration: 2000,
                                location: 'bottom'
                            });
                            that.get_data();
                            return;
                        }
                    } else {
                        api.toast({
                            msg: err,
                            duration: 2000,
                            location: 'bottom'
                        });
                    }
                });
                that.show = false;
                done();
            } else if (action === 'cancel') {
                done();
            }
        },
        //删除订单
        do_deleteorder: function (id) {
            var that = this;
            api.confirm({
                title: '提示',
                msg: '确定删除?',
                buttons: ['确定', '取消']
            }, function (ret, err) {
                if (ret.buttonIndex == 1) {
                    //调用删除接口
                    var opt = {
                        action: 'deleteorder', //删除的方法名
                        OrderID: id,
                    };
                    ns.post(opt, function (succeed, data, err) {
                        if (succeed) {
                            api.toast({
                                msg: '操作成功！',
                                duration: 2000,
                                location: 'bottom'
                            });
                            that.get_data();
                        } else if (err) {
                            api.toast({
                                msg: err,
                                duration: 2000,
                                location: 'bottom'
                            });
                        }
                    })
                }
            });
        },
        //确认送达
        do_sendokorder: function (id) {
            var that = this;
            ns.openWin('dl_order_send_ok', '确认送达', {
                id: id,
            });
        },
    }
});

apiready = function () {
    api.parseTapmode();
    ns = window.Foresight.Util;
    ns.init_pull();
    app.get_data();

    api.addEventListener({
        name: 'relod_orderlist'
    }, function (ret, err) {
        app.get_data();
    });
    //搜索
    api.addEventListener({
        name: 'do_open_order_search'
    }, function (ret, err) {
        ns.openWin('dl_search_frm', '搜索', {
            currentType: 'order'
        });
    });

}
